using System;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using ContosoDashboard.ContosoDashboardService;
using ContosoDashboard.Views;

namespace ContosoDashboard
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            Loaded += MainPageLoaded;
            NetworkChange.NetworkAddressChanged += NetworkChangeNetworkAddressChanged;
            ReactiveContosoService.Current.Customers.CollectionChanged += CustomersCollectionChanged;
        }

        private void CustomersCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                ClientSendUpdateReceived(e.NewItems[0] as CustomerInfo);
        }

        private void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
                NotificationService.Current.ShowNotification("Update success");
            else if (e.Error != null)
                NotificationService.Current.ShowNotification("Update failed.");
            else
                NotificationService.Current.ShowNotification("No available update.");
        }

        private void ClientSendUpdateReceived(CustomerInfo customer)
        {
            try
            {
                if (Application.Current.MainWindow.IsActive == false)
                    NotificationService.Current.ShowNotification("Mister " + customer.Name + " wants to rent a Dixel.");
            }
            catch
            {
                // Handle window closing exceptions
            }
        }

        private void OnNetworkAvailable()
        {
        }

        private void OnNetworkUnavailable()
        {
            NotificationService.Current.ShowNotification("Network is not availabe");
        }

        private void MainPageLoaded(object sender, RoutedEventArgs e)
        {
            Application.Current.CheckAndDownloadUpdateCompleted += Current_CheckAndDownloadUpdateCompleted;
            Application.Current.CheckAndDownloadUpdateAsync();

            if (NetworkInterface.GetIsNetworkAvailable())
                OnNetworkAvailable();
        }

        private void NetworkChangeNetworkAddressChanged(object sender, EventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
                OnNetworkAvailable();
            else
                OnNetworkUnavailable();
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrameNavigated(object sender, NavigationEventArgs e)
        {
            foreach (var child in LinksStackPanel.Children)
            {
                var hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (ContentFrame.UriMapper.MapUri(e.Uri).ToString().Equals(ContentFrame.UriMapper.MapUri(hb.NavigateUri).ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrameNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void FullScreenClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen ^= true;
        }
    }
}